/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sms4j.util;

import flexjson.JSONDeserializer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import sms4j.entity.ShortMessage;

/**
 *
 * @author Swang
 */
public class SmsUtil {
    private static final Log filelog = LogFactory.getLog("sms4j.util");
    
    public static final String SNED_STATUS_QUEUE = "Success put message in the send queue";
    
    public static String sendMessage(String jsondata) {
        ShortMessage shortMessage = new JSONDeserializer<ShortMessage>().deserialize(jsondata.trim());
        return sendMessage(shortMessage);
    }
    
    public static String sendMessage(ShortMessage shortMessage) {
        try {
            
            OutboundMessage outboundMessage = new OutboundMessage(shortMessage.getToPhoneNumber(), shortMessage.getMessage());
            outboundMessage.setEncoding(MessageEncodings.ENCUCS2); //设置为可发送中文的编码
            Service.getInstance().queueMessage(outboundMessage); //发送到队列中
            filelog.info("Put Message In Send Queue: ToPhoneNumber->"+shortMessage.getToPhoneNumber()+" Message->"+shortMessage.getMessage());
            return SNED_STATUS_QUEUE;
        } catch (Exception ex) {
            filelog.info("Error Put Message In Queue: ToPhoneNumber->"+shortMessage.getToPhoneNumber()+" Message->"+shortMessage.getMessage(),ex);
            return "Exception send message";
        }
    }
}
